const express = require('express');
const router = express.Router();
const sqlFn = require('../mysql')
const remove = require("loadsh/remove");

/**
 * @swagger
 * /menuList:
 *   get:
 *     summary: 获取菜单列表
 *     description: 获取用户的菜单列表，管理员用户可以获取所有菜单，普通用户根据关联菜单获取。
 *     tags:
 *       - Menu
 *     parameters:
 *       - name: account
 *         in: query
 *         required: true
 *         description: 用户账户，传入 "admin" 获取所有菜单。
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: 成功返回菜单列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 records:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                         description: 菜单ID
 *                       pid:
 *                         type: integer
 *                         description: 父菜单ID
 *                       name:
 *                         type: string
 *                         description: 菜单名称
 *                       children:
 *                         type: array
 *                         description: 子菜单
 *                         items:
 *                           $ref: '#/components/schemas/Menu'
 *       400:
 *         description: 请求参数错误
 *       500:
 *         description: 服务器内部错误
 */

/**
 * @swagger
 * components:
 *   schemas:
 *     Menu:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           description: 菜单ID
 *         pid:
 *           type: integer
 *           description: 父菜单ID
 *         name:
 *           type: string
 *           description: 菜单名称
 *         children:
 *           type: array
 *           items:
 *             $ref: '#/components/schemas/Menu'
 */

router.get('/menuList', function (req, res, next) {
    const getMenuTree = (menus = []) => {
        menus.map(item => {
            menus.map(menu => {
                if (item.pid == menu.id) {
                    menu.children = menu.children || []
                    menu.children.push(item)
                }
            })
        })
        remove(menus, (menu) => menu.pid != -1)
        return menus
    }
    if (req.authData.account == 'admin') {
        const sql = 'select * from menu'
        sqlFn(sql, [], function (err, data) {
            if (err) {
                next(err)
                return
            }
            res.send({code: 200, records: getMenuTree(data)})
        })
        return
    }
    const sql = 'select * from user where `account`=?'
    sqlFn(sql, [req.authData.account], function (err, data, next) {
        if (err) {
            next(err)
        }
        if (data && data.length) {
            let sql = 'select * from menu where '
            const menuIdS = (data[0].relatedMenus || '').split(',')
            menuIdS.map((item, index) => {
                sql += `id=${item} ${index == menuIdS.length - 1 ? '' : 'or '}`
            })
            sqlFn(sql, [], function (err, data) {
                if (err) {
                    next(err)
                }
                res.send({code: 200, records: getMenuTree(data)})
            })
        }
    })
})
module.exports = router;
